package com.neusoft.hifly.lock.config;

import java.io.IOException;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import com.neusoft.hifly.lock.RedissonLock;
import com.neusoft.hifly.lock.RedissonManager;
import com.neusoft.hifly.lock.utils.Log;

/**
 * Redisson自动化配置
 */
@Configuration
@ConditionalOnClass(Redisson.class) // 当spirng容器中有Redisson这个class的时候，再加载这个配置类
public class RedissonAutoConfiguration {
	@Autowired(required = false)
	private RedissonConfig redissonConfig;

	@Bean
	@ConditionalOnMissingBean
	@Order(value = 2)
	public RedissonLock redissonLock(final RedissonManager redissonManager) {
		final RedissonLock redissonLock = new RedissonLock(redissonManager);
		Log.info("[RedissonLock]组装完毕");
		return redissonLock;
	}

	@Bean
	@ConditionalOnMissingBean
	@Order(value = 1)
	public RedissonManager redissonManager() {
		Log.info("[RedissonManager]组装完毕,当前连接方式:" + redissonConfig.getType() + ",连接地址:" + redissonConfig.getAddress());
		final RedissonManager redissonManager = new RedissonManager(redissonConfig);
		return redissonManager;
	}

	@Bean
	@ConditionalOnMissingBean
	@Order(value = 3)
	public RedissonClient redissonClient() throws IOException {
		final RedissonManager redissonManager = new RedissonManager(redissonConfig);
		return redissonManager.getRedisson();
	}

}
